package comu.hnevc.zh.testdemo.dialog;

import android.app.AlertDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import comu.hnevc.zh.testdemo.R;
import comu.hnevc.zh.testdemo.dao.RechargeBean;
import comu.hnevc.zh.testdemo.dao.RechargeDao;
import comu.hnevc.zh.testdemo.base.BaseUrl;
import comu.hnevc.zh.testdemo.bean.Car;

public class AccountDialog extends AlertDialog {
    Context context;
    RechargeDao carRechargeDao;
    List<Car> carList;
    private EditText money;

    public AccountDialog(Context context, List<Car> carList) {
        super(context);
        this.context=context;
        this.carList=carList;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.account_dialog);
        TextView chepai=findViewById(R.id.tv_accountdialog_chepai);
        money=findViewById(R.id.tv_accountdialog_money);
        Button btture=findViewById(R.id.bt_accoundialog_true);
        Button btfalse=findViewById(R.id.bt_accoundialog_false);
        carRechargeDao=new RechargeDao(context);
        StringBuffer totalNumber=new StringBuffer();
        for (int i=0;i<carList.size();i++){
            Car car=carList.get(i);
            if (car.isNeedchaerge()){
                totalNumber.append(car.getCarnum());
            }
        }
        chepai.setText(totalNumber.toString());

        //取消
        btfalse.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              dismiss();
            }
        });

        btture.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for(int i=0;i<carList.size();i++){
                    Car car=carList.get(i);
                    if (car.isNeedchaerge()==true){
                        initdata(car);
                    }
                }
            }
        });
    }

    private void initdata( final Car car) {
        SharedPreferences spf=context.getSharedPreferences("data",Context.MODE_PRIVATE);
        final  String user=spf.getString("userName","user2");
        JSONObject object=new JSONObject();
        final String  rechargemoney=money.getText().toString().trim();
        if (TextUtils.isEmpty(rechargemoney)){
            Toast.makeText(context,"充值金额不能为空",Toast.LENGTH_LONG).show();
            return;
        }
        try {
            object.put("CarId",car.getCarid());
            object.put("Money",Integer.valueOf(rechargemoney));
            object.put("UserName",user);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestQueue mQueue=Volley.newRequestQueue(getContext());
        JsonObjectRequest jor=new JsonObjectRequest(Request.Method.POST, BaseUrl.BASEURL + BaseUrl.carAccountRecharge_url, object, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Log.e("车辆充值", jsonObject.toString());
                int a = Integer.valueOf(rechargemoney);
                int b = car.getCarblance();
                int c = a + b;
                car.setCarblance(c);
                //获取当前系统时间
                long longTime = System.currentTimeMillis();
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                SimpleDateFormat dateFormatlist = new SimpleDateFormat("yyyy-MM-dd");
                SimpleDateFormat dateFormatweek = new SimpleDateFormat("EEEE");

                //新增充值记录
                Log.e("insert", "insert" + b + "\n" + c);
                //获取充值信息到数据库
                RechargeBean carRechargeInfo = new RechargeBean();
                carRechargeInfo.setCar_number(car.getCarnum() + "");
                carRechargeInfo.setRe_money(rechargemoney);
                carRechargeInfo.setBalance(c + "");
                carRechargeInfo.setRe_time(dateFormat.format(new Date(longTime)));
                carRechargeInfo.setWeek(dateFormatweek.format(new Date(longTime)));
                carRechargeInfo.setList_time(dateFormatlist.format(new Date(longTime)));
                carRechargeDao.insert(carRechargeInfo);

                Toast.makeText(context,"充值成功",Toast.LENGTH_SHORT).show();
                dismiss();


            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        });
        mQueue.add(jor);

    }
}
